package com.sora.sora.security.authorise.service.impl;

import com.github.pagehelper.PageHelper;
import com.sora.sora.common.model.bo.RoleBasicConvert;
import com.sora.sora.security.authorise.mapper.RoleMapper;
import com.sora.sora.security.authorise.model.entity.RoleEntity;
import com.sora.sora.security.authorise.model.param.RoleParam;
import com.sora.sora.security.authorise.service.RoleService;
import com.sora.sora.server.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service("rolrService")
public class RoleServiceImpl implements RoleService {

    @Resource
    RoleMapper roleMapper;

    @Resource
    UserMapper userMapper;


    @Override
    public int createRole(RoleParam roleParam) {
        RoleEntity roleEntity = RoleBasicConvert.INSTANCE.paramToEntity(roleParam);
        roleEntity.setCreateTime(new Date());
        roleMapper.addRole(roleEntity);
        List resourceResult = new LinkedList();
        int i = 0;
        for (int resourceId : roleParam.getResourcesId()) {
            i = roleMapper.addResourceRole(roleEntity.getId(), resourceId);
            if (i <= 0) {
                resourceResult.add(i);
            }
        }
        int uid = userMapper.queryIdByName(roleEntity.getName());
        return roleMapper.addUserRole(uid, roleEntity.getId());
    }

    @Override
    public int updateRole(RoleParam roleParam) {
        RoleEntity roleEntity = RoleBasicConvert.INSTANCE.paramToEntity(roleParam);
        roleEntity.setCreateTime(new Date());
        return roleMapper.updateRole(roleEntity);
    }

    @Override
    public List<Integer> delRole(List<Integer> roleIds) {
        List<Integer> failure = new ArrayList<>();
        int result;
        for (Integer roleId : roleIds) {
            result = roleMapper.delRoleByRoleId(roleId);
            System.out.println(result);
            if (result <= 0) {
                failure.add(roleId);
            }
        }
        return failure;
    }

    @Override
    public List<RoleEntity> listRoles(int page, int limit) {

        PageHelper.startPage(page, limit);
        List<RoleEntity> list = roleMapper.queryAll();
        return list;
    }

    @Override
    public int getCount() {
        return roleMapper.queryCount();
    }

    @Override
    public List<Integer> grantUserRole(List<Integer> uids, Integer roleId) {
        List<Integer> failure = new ArrayList<>();
        int result;
        for (Integer uid : uids) {
            result = roleMapper.addUserRole(uid, roleId);
            if (result <= 0) {
                failure.add(uid);
            }
        }
        return failure;
    }

    @Override
    public List<Integer> cancleGrantUserRole(List<Integer> uids, Integer roleId) {
        List<Integer> failure = new ArrayList<>();
        int result;
        for (Integer uid : uids) {
            result = roleMapper.delUserRole(uid, roleId);
            if (result <= 0) {
                failure.add(uid);
            }
        }
        return failure;
    }
}
